package com.xtx.air.jcaptcha;

import com.octo.captcha.CaptchaException;
import com.octo.captcha.CaptchaQuestionHelper;
import com.octo.captcha.component.image.wordtoimage.WordToImage;
import com.octo.captcha.component.word.wordgenerator.WordGenerator;
import com.octo.captcha.image.ImageCaptcha;
import com.octo.captcha.image.gimpy.GimpyFactory;

import java.awt.image.BufferedImage;
import java.util.Locale;

/**
 * User: hanyi
 * Date: 12-2-20
 * Time: 下午9:23
 */
public class GimpyIgnoreCaseFactory extends GimpyFactory {
    public GimpyIgnoreCaseFactory(WordGenerator generator, WordToImage word2image) {
        super(generator, word2image);
    }

    @Override
    public ImageCaptcha getImageCaptcha(Locale locale) {
        Integer wordLength = getRandomLength();

        String word = getWordGenerator().getWord(wordLength, locale);

        BufferedImage image = null;
        try {
            image = getWordToImage().getImage(word);
        } catch (Throwable e) {
            throw new CaptchaException(e);
        }

        ImageCaptcha captcha = new GimpyIgnoreCase(CaptchaQuestionHelper.getQuestion(locale, BUNDLE_QUESTION_KEY), image, word);

        return captcha;
    }
}
